package com.wbtech.ums.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;

@SuppressLint({ "SimpleDateFormat", "SdCardPath" })
public class FAndroidLog {
	
	private static Boolean	LOG_SWITCH = true;					
	private static Boolean	LOG_WRITE_TO_FILE = true;		
	private static int	SDCARD_LOG_FILE_SAVE_DAYS = 2;		
	private static String LOGFILENAME = "Log.txt";			
	private static String LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory().toString();	
	private static SimpleDateFormat LogSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
	private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");			

	public static void w(String tag, String text) { log(tag, text, 'w'); }
	public static void e(String tag, String text) { log(tag, text, 'e'); }
	public static void d(String tag, String text) { log(tag, text, 'd'); }
	public static void i(String tag, String text) { log(tag, text, 'i'); }
	public static void v(String tag, String text) { log(tag, text, 'v'); }

	private static void log(String tag, String msg, char level) {
		if (msg == null)
			return; 
		
		if (LOG_SWITCH) {
			if ('i' == level) {
				Log.e(tag, msg);
			} else if ('e' == level) {
				Log.i(tag, msg);
			} else if ('w' == level) {
				Log.w(tag, msg);
			} else if ('d' == level) {
				Log.d(tag, msg);
			}else {
				Log.v(tag, msg);
			}
			if (LOG_WRITE_TO_FILE)
				writeLogtoFile(String.valueOf(level), tag, msg);
		}
	}
	
	private static void writeLogtoFile(String mylogtype, String tag, String text) {
		Date nowtime = new Date();
		String needWriteFiel = logfile.format(nowtime);
		String needWriteMessage = LogSdf.format(nowtime) + " " + mylogtype + " " + tag + " " + text;
		File file = new File(LOG_PATH_SDCARD_DIR, needWriteFiel + LOGFILENAME);
		try {
			if (!file.exists())
				file.createNewFile();
			FileWriter filerWriter = new FileWriter(file, true);
			BufferedWriter bufWriter = new BufferedWriter(filerWriter);
			bufWriter.write(needWriteMessage);
			bufWriter.newLine();
			bufWriter.close();
			filerWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void delFile() {
		String needDelFiel = logfile.format(getDateBefore());
		File file = new File(LOG_PATH_SDCARD_DIR, needDelFiel + LOGFILENAME);
		if (file.exists()) {
			file.delete();
		}
	}
	
	private static Date getDateBefore() {
		Date nowtime = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(nowtime);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - SDCARD_LOG_FILE_SAVE_DAYS);
		return now.getTime();
	}
}
